<template>  
  <view class="page-container">   
    <image class="bg-image" src="/static/image.jpg" mode="aspectFill"></image>
    
    <view class="content">
      <view class="nav-bar">
        <view class="back-btn" @click="goBack">
          <image src="/static/return.png" mode="aspectFit" class="back-icon"/>
        </view>
        <text class="nav-title">注册账号</text>
      </view>

      <form>  
        <view class="input-group">  
          <text class="label">用户名</text>  
          <input 
            class="input" 
            v-model="user_name" 
            placeholder="请输入用户名"
            placeholder-class="placeholder" 
          />  
        </view>  

        <view class="input-group">  
          <text class="label">账号</text>  
          <input 
            class="input" 
            v-model="user_account" 
            placeholder="请输入账号"
            placeholder-class="placeholder" 
          />  
        </view>  

        <view class="input-group">  
          <text class="label">密码</text>  
          <view class="password-container">
            <input
              v-model="user_password"  
              class="input"  
              placeholder="请输入密码"  
              placeholder-class="placeholder"
              :type="showPassword ? 'text' : 'password'"  
            />  
            <image
              class="password-toggle"
              :src="showPassword ? '/static/btn_hide_login.png' : '/static/btn_visible_login.png'"
              @click="togglePassword"
              mode="aspectFit"
            />
          </view>
        </view>  

        <view class="input-group">  
          <text class="label">确认密码</text>  
          <view class="password-container">
            <input
              v-model="confirmPassword"  
              class="input"  
              placeholder="请再次输入密码"  
              placeholder-class="placeholder"
              :type="showConfirmPassword ? 'text' : 'password'"  
              @input="checkPassword"
            />  
            <image
              class="password-toggle"
              :src="showConfirmPassword ? '/static/btn_hide_login.png' : '/static/btn_visible_login.png'"
              @click="toggleConfirmPassword"
              mode="aspectFit"
            />
          </view>
        </view>

        <text v-if="passwordError" class="error-tip">两次输入的密码不一致</text>

        <button type="submit" class="submit-button" @click="onSubmit">注册</button>  
      </form>  
    </view>
  </view>  
</template>  

<script>
import { register } from '/api/user.js'

export default {
    data() {
        return {
            user_name: '',
            user_account: '',
            user_password: '',
            confirmPassword: '',
            showPassword: false,
            showConfirmPassword: false,
            passwordError: false
        }
    },

    methods: {
        togglePassword() {
            this.showPassword = !this.showPassword
        },

        toggleConfirmPassword() {
            this.showConfirmPassword = !this.showConfirmPassword
        },

        checkPassword() {
            if (this.confirmPassword && this.user_password !== this.confirmPassword) {
                this.passwordError = true
            } else {
                this.passwordError = false
            }
        },

        goBack() {
            uni.navigateBack()
        },

        async onSubmit() {
            // 表单验证
            if (!this.user_name || !this.user_account || !this.user_password || !this.confirmPassword) {
                uni.showToast({
                    title: '请填写完整信息',
                    icon: 'none'
                })
                return
            }

            // 检查密码是否一致
            if (this.user_password !== this.confirmPassword) {
                uni.showToast({
                    title: '两次输入的密码不一致',
                    icon: 'none'
                })
                return
            }

            // 构造注册数据
            const formData = {
                user_name: this.user_name,
                user_account: this.user_account,
                user_password: this.user_password
            }

            try {
                const res = await register(formData)
                if (res.data.code === 2000) {
                    // 只显示注册成功提示，不跳转
                    uni.showToast({
                        title: '注册成功',
                        icon: 'success',
                        duration: 1500
                    })
                } else {
                    uni.showToast({
                        title: res.message || '注册失败',
                        icon: 'none'
                    })
                }
            } catch (error) {
                console.error('注册失败:', error)
                uni.showToast({
                    title: '注册失败，请重试',
                    icon: 'none'
                })
            }
        }
    }
}
</script>  

<style scoped>
.page-container {
  width: 100%;
  height: 100vh;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

.bg-image {
  position: absolute;
  width: 100%;
  height: 100vh;
  top: 0;
  left: 0;
  z-index: 1;
}

.content {
  position: relative;
  z-index: 2;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 20px;
}

.input-group {  
  margin-bottom: 20px;  
}  

.label {
  font-size: 14px;
  color: #333;
  margin-bottom: 8px;
  display: block;
  font-weight: 500;
}

.input {
  width: 92%;
  height: 45px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  padding: 0 15px;
  font-size: 14px;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(5px);
}

.code-group {  
  display: flex;  
  align-items: center;
  gap: 10px;
}  

.code-input {
  flex: 1;
}

.code-button {  
  width: 110px;
  height: 45px;
  background: rgba(107, 75, 138, 0.8);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  backdrop-filter: blur(5px);
}

.button-disabled {
  background: rgba(204, 204, 204, 0.8);
}

.password-container {
  position: relative;
  margin-bottom: 4px;  /* 为错误提示预留空间 */
}

.password-toggle {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  cursor: pointer;
}

.submit-button {  
  background: rgba(107, 75, 138, 0.8);
  color: white;
  height: 45px;
  border: none;
  border-radius: 25px;
  width: 100%;
  font-size: 16px;
  font-weight: 500;
  margin-top: 40px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  backdrop-filter: blur(5px);
}  

.submit-button:active {
  transform: scale(0.98);
}

/* 添加导航栏样式 */
.nav-bar {
  display: flex;
  align-items: center;
  padding: 10px 20px;
  margin-bottom: 20px;
}

.back-btn {
  padding: 30px;
  margin-left: -50px;
}

.back-icon {
  width: 30px;
  height: 30px;
}

.nav-title {
  flex: 1;
  text-align: center;
  font-size: 18px;
  font-weight: 500;
  margin-right: 40px;
  color: #333;
}

.error-tip {
  font-size: 12px;
  color: #ff4d4f;
  margin-top: 4px;
  padding-left: 10px;
}
</style>